package com.example.tool.activtiy;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.graphics.Matrix;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;

import com.example.tool.R;
import com.example.tool.activtiy.base.BaseActivity;
import com.example.tool.mvp.CommonPresent;
import com.example.tool.widget.MatrixView;

import butterknife.BindView;

/**
 * https://github.com/aserbao/AndroidCamera
 */
public class TikTokActivity extends BaseActivity<CommonPresent> {
    @BindView(R.id.change)
    Button button;

    @BindView(R.id.iv_test)
    MatrixView matrixView;

//    @SuppressLint("HandlerLeak")
//    private Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            switch (msg.what) {
//                case 10000:
//                    fab += 20;
//                    czy += 20;
//                    anin(fab, czy);
//                    handler.sendEmptyMessageDelayed(10000, 500);
//                    break;
//            }
//        }
//    };


    @Override
    protected int getLayoutId() {
        return R.layout.activity_tiktok2;
    }

    @Override
    protected void initView() {

    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setTranslateAnimation();

            }
        });
    }


    @Override
    protected CommonPresent createPresenter() {
        return null;
    }

    @Override
    protected void onResume() {
        super.onResume();

    }


    private class MyTransXAnimatorListener implements ValueAnimator.AnimatorUpdateListener {


        private Matrix mPrimaryMatrix;

        public MyTransXAnimatorListener(Matrix matrix) {

            mPrimaryMatrix = new Matrix(matrix);

        }

        @Override

        public void onAnimationUpdate(ValueAnimator animation) {

            int dx = (Integer) animation.getAnimatedValue();

            Matrix matrix = new Matrix(mPrimaryMatrix);

            matrix.postTranslate(dx, 0);

            matrixView.change(matrix, R.mipmap.kun01);

        }

    }

    public void setTranslateAnimation() {

        ValueAnimator animator = ValueAnimator.ofInt(0, 260);

        animator.addUpdateListener(new MyTransXAnimatorListener(matrixView.getImageMatrix()));
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                setScaleAnimation();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        animator.setDuration(1000);

        animator.setInterpolator(new LinearInterpolator());

        animator.setStartDelay(500);

        animator.start();

    }

    //
//
    private class MyScaleAnimatorListener implements ValueAnimator.AnimatorUpdateListener {

        private Matrix mPrimaryMatrix;

        public MyScaleAnimatorListener(Matrix matrix) {

            mPrimaryMatrix = matrix;

        }

        @Override

        public void onAnimationUpdate(ValueAnimator animation) {

            float scale = (Float) animation.getAnimatedValue();

            Matrix matrix = new Matrix(mPrimaryMatrix);

            matrix.postScale(scale, scale, matrixView.getWidth() - 100, matrixView.getHeight() / 4f);

            matrixView.change(matrix,R.mipmap.kun01);

        }

    }

    public void setScaleAnimation() {

        ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 2f);

        animator.addUpdateListener(new MyScaleAnimatorListener(matrixView.getImageMatrix()));

        animator.setDuration(1000);

//        animator.setInterpolator(new DecelerateInterpolator());
        animator.setInterpolator(new LinearInterpolator());

        animator.setStartDelay(500);

        animator.start();

    }

//
//    @Override
//    protected void onPause() {
//        super.onPause();
//        handler.removeCallbacksAndMessages(null);
//    }
}
